package com.chhuang.core.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * springmvc全局异常处理器
 * @author CHHUANG
 *
 */
public class SysExceptionReslver implements HandlerExceptionResolver {

	private static final String MESSAGE = "message";
	private static final String UNKNOW_ERROR = "未知错误";
	/**
	 * 解析出异常类型
	 * 如果是自定义的ExpectException，直接取出异常信息，在错误页面展示
	 * 如果不是，构造一个自定义异常类型（信息“未知错误”）
	 */
	@Override
	public ModelAndView resolveException(HttpServletRequest request, 
			HttpServletResponse response, Object obj, Exception e) {
		ExpectException ee = null;
		if(e instanceof ExpectException){
			ee = ((ExpectException)e);
		}else{
			ee = new ExpectException(UNKNOW_ERROR);
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject(MESSAGE, ee.getMessage());
		modelAndView.setViewName("error/500");
		return modelAndView;
	}
	
}
